package com.hankol.material.adapter

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.HKUtils
import com.baselibrary.utils.ToastUtils
import com.hankol.material.MaterialMainHomeActivity
import com.hankol.material.R
import com.hankol.material.api.MaterialConstants
import com.hankol.material.api.OnTouchOutsideViewListener
import com.hankol.material.bean.FolderListBean
import com.hankol.material.viewmodel.MaterialBaseViewModel
import java.util.*

/**
 * @description:
 * @author: 关忆北丶
 * @date :   2021/10/27 11:05
 */
class HomeFolderListAdapter(private val activity: MaterialMainHomeActivity, private val viewModel: MaterialBaseViewModel) : RecyclerView.Adapter<HomeFolderListAdapter.HomeFolderListHolder>() {
    private val mList = ArrayList<FolderListBean>()
    private var signPosition = -1
    private var showDeleteFolder: PopupWindow? = null
    fun setData(mList: List<FolderListBean>) {
        this.mList.clear()
        this.mList.addAll(mList)
        notifyDataSetChanged()
    }

    fun addData(mList: List<FolderListBean>) {
        this.mList.addAll(mList)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeFolderListHolder {
        val inflate = LayoutInflater.from(activity).inflate(R.layout.item_folder_list, parent, false)
        return HomeFolderListHolder(inflate)
    }

    override fun onBindViewHolder(holder: HomeFolderListHolder, position: Int) {
        if (position == itemCount - 1) {
            holder.mLlHomeItemFolder.visibility = View.GONE
            holder.mIvHomeItemAddFolder.visibility = View.VISIBLE
        } else {
            holder.mLlHomeItemFolder.visibility = View.VISIBLE
            holder.mIvHomeItemAddFolder.visibility = View.GONE
        }
        holder.mIvHomeItemDelete.visibility = View.INVISIBLE
        holder.mIvHomeItemEdt.visibility = View.GONE
        if (position < mList.size) {
            holder.mTvHomeItemName.text = mList[position].name
        }

        holder.mLlHomeItemFolder.setOnClickListener {
            if (!HKUtils.isFastClick()) return@setOnClickListener
            val mBundle = Bundle()
            mBundle.putSerializable(MaterialConstants.FOLDER_DETAIL, mList[position])
            ActivityRouter.startActivity(ActivityRouter.MATERIAL.MATERIAL_COLLECTION_FOLDER, mBundle)
        }

        holder.mIvHomeItemAddFolder.setOnClickListener {
            if (!HKUtils.isFastClick()) return@setOnClickListener
            ActivityRouter.startActivity(ActivityRouter.MATERIAL.MATERIAL_CREATE_FOLDER)
        }
        holder.mLlHomeItemFolder.setOnLongClickListener {
            if (mList[position].name == MaterialConstants.DEF_FOLDER_NAME || position == itemCount - 1)
                return@setOnLongClickListener true
            activity.setOnTouchOutsideViewListener(holder.itemView, OnTouchOutsideViewListener { view, event ->
                holder.mIvHomeItemDelete.visibility = View.INVISIBLE
                holder.mIvHomeItemEdt.visibility = View.GONE
            })

            if (holder.mIvHomeItemDelete.visibility == View.INVISIBLE && holder.mIvHomeItemEdt.visibility == View.GONE) {
                //ScreenUtils.sendPhoneShock(activity, 50)
                holder.mIvHomeItemDelete.visibility = View.VISIBLE
                holder.mIvHomeItemEdt.visibility = View.VISIBLE
            }
            return@setOnLongClickListener true
        }

        holder.mIvHomeItemDelete.setOnClickListener {
            showDeleteFolder = viewModel.showDeleteFolder(mList[position].id, activity)
            signPosition = position
        }
        holder.mIvHomeItemEdt.setOnClickListener {
            if (!HKUtils.isFastClick()) return@setOnClickListener
            val bundle = Bundle()
            bundle.putSerializable(MaterialConstants.FOLDER_CONTENT, mList[position])
            ActivityRouter.startActivity(ActivityRouter.MATERIAL.MATERIAL_CREATE_FOLDER, bundle)
            signPosition = position
        }
    }

    fun upItemData(bean: FolderListBean) {
        if (signPosition != -1) {
            mList[signPosition] = bean
            notifyItemChanged(signPosition)
            signPosition = -1
            ToastUtils.showShort(activity.applicationContext, "修改成功")
        }
    }

    /**
     * 删除被点击的条目
     */
    fun deleteItemData() {
        mList.removeAt(signPosition)
        notifyDataSetChanged()
        signPosition = -1
        showDeleteFolder?.apply {
            if (isShowing) dismiss()
        }
    }

    override fun getItemCount(): Int {
        return mList.size + 1
    }


    inner class HomeFolderListHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var mIvHomeItemFolder: ImageView = itemView.findViewById(R.id.iv_homeItem_folder)
        var mTvHomeItemName: TextView = itemView.findViewById(R.id.tv_homeItem_name)
        var mLlHomeItemFolder: LinearLayout = itemView.findViewById(R.id.ll_homeItem_folder)
        var mIvHomeItemAddFolder: ImageView = itemView.findViewById(R.id.iv_homeItem_addFolder)
        var mIvHomeItemDelete: LinearLayout = itemView.findViewById(R.id.iv_homeItem_delete)
        var mIvHomeItemEdt: ImageView = itemView.findViewById(R.id.iv_homeItem_edt)
    }
}
